package generator

import generator.common.FreeMarker
import generator.entity.testInfo
import generator.util.dataBaseConnect

import java.io.File
import scala.io.Source
import scala.util.matching.Regex

object test {

  def getFieldFromSetFunc(str: String): String = {
    var funcName = str.substring(3)
    val regex = new Regex("([A-Z])")
    regex.replaceAllIn(funcName,matcher=>{
      "_"+matcher.group(1)
    }).substring(1)
  }

  def main(args: Array[String]): Unit = {
//    dataBaseConnect.connect("c_cons_prc")
    val marker = new FreeMarker()
    val empty = "\\s*\n*\\s*"

    // 每次修改
    //var regex = new Regex("if"+empty+"\\(j_bean.getType\\(\\).equals\\(\"D\"\\)\\)"+empty+"\\{"+empty+"//d_bean.set[a-zA-Z]+\\(j_bean.getBefore\\(\\).get[a-zA-Z_]+\\(\"[a-zA-z_]+\".toUpperCase\\(\\)\\)\\);"+empty+"d_bean.setType\\(j_bean.getType\\(\\)\\);"+empty+"\\}"+empty+"else"+empty+"\\{"+empty+"(("+empty+"d_bean.set[a-zA-Z0-9]+\\(j_bean.getAfter\\(\\).get[a-zA-Z]+\\(\"[a-zA-Z_]+\".toUpperCase\\(\\)\\)\\);"+empty+")+)")

    var regex = new Regex("[^@]+"+ "@"+empty+"\\{"+empty+"(("+empty+"#d_bean.set[a-zA-Z0-9]+\\(j_bean.getAfter\\(\\).get[a-zA-Z]+\\(\"[a-zA-Z_]+\".toUpperCase\\(\\)\\)\\);[^#]+)+)")
 //    var regex = new Regex("([^\n]+\n[^\n]+)\n+ORA-[^\n]+\n")

    var textFile = new File("datas/test.txt")
    val content = Source.fromFile(textFile).mkString.replace("\r","").replace("else","@").replaceAll("//","#")


    // 组合逻辑
    val value = regex.findFirstMatchIn(content).get
    val str = value.group(1)
    val regex2 = new Regex(empty+"#(d_bean.(set[a-zA-Z0-9]+)\\(j_bean.getAfter\\(\\).get([a-zA-Z]+)\\(\"([a-zA-Z_]+)\".toUpperCase\\(\\)\\)\\);)")
    val list = regex2.findAllMatchIn(str).map(matcher=>(matcher.group(1),matcher.group(2),matcher.group(3),matcher.group(4),dataBaseConnect.isNullable("c_mp",getFieldFromSetFunc(matcher.group(2))))).toList

//    val list = regex.findAllMatchIn(content).map(_.group(1)).toList

    //数据模型
//    val dataModel = testInfo("null",list)
    val dataModel = testInfo(null,list)

    marker.makeTemplate("test.ftl",dataModel,"datas/testOut.txt")
  }
}
